//
//  MainViewController.swift
//  weibo1
//
//  Created by changhongliang on 2017/7/21.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
         //设置tabbar文字
        setupItemTextAttrs()
        //添加子控制器
        setUpChildVC();
        //设置TabBar
        self.setValue(CustomTabBar(), forKey: "tabBar")
        let tabBar = self.tabBar as! CustomTabBar
        tabBar.centerBtn.addTarget(self, action:#selector(MainViewController.centerBtnClick), for: .touchUpInside)
       
        


    }
    
    func  setUpChildVC(){
        addChildViewController(childVC: HomeViewController(), titleName:"微博", imageName:"tabbar_home")
        addChildViewController(childVC: MessageController(), titleName:"消息", imageName: "tabbar_message_center")
        addChildViewController(childVC: DiscoverController(), titleName: "发现", imageName:"tabbar_discover")
        addChildViewController(childVC: ProfileController(), titleName: "我", imageName: "tabbar_profile")

    }
    
    
    func setupItemTextAttrs() {
        var normalAttrs:Dictionary<String,AnyObject> = Dictionary<String,AnyObject>()
        normalAttrs[NSForegroundColorAttributeName] = UIColor.black
        normalAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 10)
        
        var selectedAttrs:Dictionary<String,AnyObject> = Dictionary<String,AnyObject>()
        selectedAttrs[NSForegroundColorAttributeName] = UIColor.orange
        selectedAttrs[NSFontAttributeName] = UIFont.systemFont(ofSize: 10)
        
        let item = UITabBarItem.appearance()
        item.setTitleTextAttributes(normalAttrs, for: UIControlState.normal)
        item.setTitleTextAttributes(selectedAttrs, for: UIControlState.selected)
    }
    
    
    func addChildViewController(childVC:UIViewController, titleName:String,imageName:String){
        childVC.title = titleName
        let navVC = BaseNavigationController(rootViewController:childVC)
        navVC.tabBarItem.title = titleName
        navVC.tabBarItem.image = UIImage(named:imageName)
        navVC.tabBarItem.selectedImage = UIImage(named:imageName + "_highlighted")
        addChildViewController(navVC)
    }
    
    
    
   // MARK: - 内部控制方法
   // 如果单纯使用'private'运行循环将无法正常发送消息，导致崩溃
   // 如果使用 @objc 修饰符(动态派发)，可以保证运行循环能够发送此消息，即使函数被标记为private
    @objc private func centerBtnClick()  {
        let composeVC = ComposeViewController()
        let nav = UINavigationController(rootViewController: composeVC)
        present(nav, animated: true, completion: nil)

    }
    
       
}
